package com.v1.video.util;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.provider.MediaStore.Video.Thumbnails;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.io.File;
import java.io.FileInputStream;
import java.util.Hashtable;
import java.util.Vector;


public class PicUtils {

    public static final Bitmap rotate(int angle, Bitmap bitmap) {

        Matrix matrix = new Matrix();
        matrix.postRotate(angle);

//        Bitmap.createBitmap(source, x, y, width, height, m, filter)
        Bitmap bitmap2 = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(),
                bitmap.getHeight(), matrix, true);
        PicUtils.RecycledBitmap(bitmap);
        return bitmap2;      
    }
    
    public static Bitmap rotate2(Bitmap bm, final int orientationDegree){
    	 Matrix m = new Matrix();
         m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
         float targetX, targetY;
         if (orientationDegree == 90) {
	         targetX = bm.getHeight();
	         targetY = 0;
         } else {
	         targetX = bm.getHeight();
	         targetY = bm.getWidth();
         }

	     final float[] values = new float[9];
	     m.getValues(values);
	
	     float x1 = values[Matrix.MTRANS_X];
	     float y1 = values[Matrix.MTRANS_Y];
	
	     m.postTranslate(targetX - x1, targetY - y1);
	
//	     Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
	     Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_4444);
	     Paint paint = new Paint();
	     Canvas canvas = new Canvas(bm1);
	     canvas.drawBitmap(bm, m, paint);
	
	     return bm1;
    }
    
    public static final byte COLORSIZE = 4;
    public static long MaxSize = 15000000;
    public static long curSize = 0;
    public static Vector vecPngKey = new Vector();
    public static Hashtable hashContainerPng = new Hashtable(); 
    public static Bitmap getBitmapInSDCardAndCache(String path,int type,float scale){
        
        Bitmap bitmap = null;
        String keyValue = path;
        File file = null;
        FileInputStream inputStream = null;
        if(!hashContainerPng.isEmpty()){ 
            
            bitmap = (Bitmap)hashContainerPng.get(keyValue);
            
            if(bitmap !=null)
            {
                vecPngKey.removeElement(keyValue);
                vecPngKey.addElement(keyValue);
//              Log.i(TAG, "此时有bitmap");
                return bitmap;
            }
        }
        try {
            bitmap = ThumbnailUtils.createVideoThumbnail(path, Thumbnails.MICRO_KIND);
            if(bitmap != null){
                hashContainerPng.put(keyValue, bitmap);
                vecPngKey.addElement(keyValue);
                curSize += pngSize(bitmap.getWidth(),bitmap.getHeight());
                isOutSize();
            }
            return bitmap;
            
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }finally{
            try{
                if(inputStream != null){
                    inputStream.close();
                }
                inputStream = null;             
                file = null;
            }catch(Exception e){
            }
        }
    }
    
    public static int pngSize(int w,int h){
        int result = 0;
        result = w * h * COLORSIZE;
        return result;
    }   
    
    public static void isOutSize()
    {
        while(curSize > MaxSize){
            if(vecPngKey.size() <= 0){
                break;
            }
            String path = String.valueOf(vecPngKey.elementAt(0));
            Bitmap bm = (Bitmap)hashContainerPng.get(path);
            int size = pngSize(bm.getWidth(),bm.getHeight());
            curSize -= size;
            hashContainerPng.remove(path);
            vecPngKey.remove(0);
        }
    }
    
    /**
     * 及时回收不要的bitmap对象，以免内存溢出。
     * 每进程图像内存大小不能超出8M，否则内存溢出异常
     * @param bitmap
     */
	public static void RecycledBitmap(Bitmap bitmap) {
		if (bitmap != null && !bitmap.isRecycled()) {
			// 回收并且置为null
			bitmap.recycle();
			bitmap = null;
		}
		System.gc();
	}
	
	/**
	 * 清理图片缓存，关于ImageLoader中的；
	 */
	public static void clearImgCache(){
		//ImageLoader.getInstance().clearDiscCache();
    	ImageLoader.getInstance().clearMemoryCache();
	}
}
